
// Button - Implement the functionality of simple button.
// inherit: Control

// options ---

jControl.Button = jControl.create("Button");

jControl.Button.prototype.defaultOption = function() {
    this.option.text = null;
    this.option.roundCorner = { radius: "5px" };
    this.option.gradient = null;
    this.option.shadow = null;

    this.option.containerStyle = null;

    this.events = ["onClick", "onMouseEnter", "onMouseLeave"];  // event definitions

    this.option.recordEvents = { self: ["click"] };
}

jControl.Button.prototype.initialize = function() {

    jControl.disableSelection(this.container);

    this.backgroundImage = jControl.getStyleFromCascade({ "": this.option.containerStyle }, "backgroundImage");

    if (!this.backgroundImage) {
        if (jControl.cssEngine) {
            var effectStyle = jControl.combineEffect(this.option.roundCorner, this.option.gradient, this.option.shadow);
            effectStyle["jcButton"] = this.option.containerStyle;
            this.setContainerStyle("jcButton", effectStyle);
        }
        else {
            this.setContainerStyle("jcButton", this.option.containerStyle);
            this.setCss(jControl.getRoundCornerClass(this.option.roundCorner));
            this.setCss(jControl.getGradientClass(this.option.gradient));
            this.setCss(jControl.getShadowClass(this.option.shadow));
        }

        jControl.setDivText(this.container, (this.option.text == null) ? "Button" : this.option.text); // no image, set text as label
    }
    else {
        this.setContainerStyle("jcButton", this.option.containerStyle);
        jControl.setDivText(this.container, "");
        if (!this.option.tip) this.container.title = this.option.text || "";
        if (jControl.cssEngine)
            $(this.container).addClass(jControl.createStyle({ padding: 0, borderStyle: "none", backgroundColor: "transparent" })); // ignore padding and border if picture button
        else
            jControl.setCss(this.container, { padding: 0, borderStyle: "none", backgroundColor: "transparent" }); // ignore padding and border if picture button
    }
    this.enable();
}

jControl.Button.prototype.setupApi = function() {
    var oThis = this;

    this.api = $.extend(this.api, {

        // method ---
        changeStyle: function(style) {
            oThis.option.containerStyle = oThis.option.containerStyle || {};
            $.extend(oThis.option.containerStyle, style);
            oThis.initialize();
        }
    });
}

jControl.Button.prototype.getter = function(targetName, param) {
    if (!targetName) return null;

    switch (targetName.toLowerCase()) {
        case "text": return  jControl.getDivText(this.container);
    }
}

jControl.Button.prototype.setter = function(targetName, value) {
    if (!targetName) return;

    switch (targetName.toLowerCase()) {
        case "text": {   jControl.setDivText(this.container, value); break; }; //value is text
    }
}

jControl.Button.prototype.bindEvent = function() {
    var oThis = this;
    this.eventHandlers = {
        click: function(e) {
            oThis.fireEvent("onClick", e);
        },
        mouseEnter: function(e) {
            if (!oThis.backgroundImage) {
                oThis.buttonStyleBack = jControl.backupStyle(oThis, "jcButton_hover", oThis.option.hoverStyle);
                jControl.applyBackupStyle(oThis, oThis.buttonStyleBack, true);
            }
            oThis.fireEvent("onMouseEnter", e);
        },
        mouseLeave: function(e) {
            if (!oThis.backgroundImage) {
                if (oThis.buttonStyleBack) {
                    jControl.applyBackupStyle(oThis, oThis.buttonStyleBack, false);
                }
            }
            oThis.fireEvent("onMouseLeave", e);
        }
    }
    this.bind("click", function(e) { oThis.record("click", { e: e }); oThis.eventHandlers.click(e) });
    this.bind("mouseenter", function(e) {
        oThis.record("mouseEnter", { e: e }); oThis.eventHandlers.mouseEnter(e)
    });
    this.bind("mouseleave", function(e) {
        oThis.record("mouseLeave", { e: e }); oThis.eventHandlers.mouseLeave(e)
    });
}

jControl.Button.prototype.enable = function() {
    jControl.ControlBase.prototype.enable.call(this);
    this.css({ cursor: "pointer" });
}

jControl.Button.prototype.test = function(action) {
    action = action.toLowerCase();
    if (action == "mouseenter")
        this.eventHandlers.mouseEnter();
    else if (action == "mouseleave")
        this.eventHandlers.mouseLeave();
    else if (action == "click")
        this.eventHandlers.click();
}
